package com.stars.empoly.activity

import android.Manifest
import android.os.Bundle
import com.eazypermissions.common.model.PermissionResult
import com.eazypermissions.coroutinespermission.PermissionManager
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.empoly.base.MBActivity
import com.stars.empoly.databinding.ActivitySettingBinding
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.jupsh.componnent.JpushUtils
import com.stars.empoly.update.showUpgradeDialog
import com.stars.empoly.utils.PopuTipsUtils
import com.stars.empoly.vmodel.SettingVModel
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BApp
import com.trailer.base.utils.DialUtils
import com.trailer.base.utils.Toasts
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

/**
 *on 2021/5/15
 */
class SettingActivity : MBActivity<SettingVModel, ActivitySettingBinding>()  {



    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.getRate()

        vm.bean.observe(this){

            var code= BApp.instance()?.config()?.getVersionCode()
            if(code!!<it.version) {
                showUpgradeDialog(it)
            }else{
                toast("您已是最新版本！")
            }
        }


    }

    override fun listeners() {
        super.listeners()

        binding.llPhone.onClick {
            //拨打电话
            launch {
                val requestPermissions = PermissionManager.requestPermissions(
                    this@SettingActivity, 1000, Manifest.permission.CALL_PHONE
                )
                if (requestPermissions is PermissionResult.PermissionGranted) {
                    //拨打电话
                    vm.phone.value?.let {
                        DialUtils.dialPhone(context(), it)
                    }
                }
            }
        }
        binding.llToUpdate.onClick {
            //检查更新
            vm.getAppConfig()

//           UpdateUtils.checkUpgrade(true,false)

        }

        binding.llDestruction.onClick {
            //注销手机号

            PopuTipsUtils.show(context(),"确定注销账",binding.llDestruction,object : ChoiceData {
                override fun onRestuse(id: String?) {
                    vm.toDestruct()
                }
            })

        }

        binding.butExit.onClick {
            PopuTipsUtils.show(context(),"确定退出登录么",binding.butExit,object : ChoiceData {
                override fun onRestuse(id: String?) {
                    //退出登录

                    var id=MMKV.defaultMMKV().getString("userId", "")
                    JpushUtils.setClearAlias(context = applicationContext,id!!)


                    MMKV.defaultMMKV().putString("token", "")
                    UiSwitch.single_new_task_with_clear_task(context(), OneLoginActivity::class.java)
                }
            })
        }


    }
    override fun vmClass(): Class<SettingVModel> {
        return SettingVModel::class.java
    }
    override fun binding(): ActivitySettingBinding {
       return ActivitySettingBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "设置"
    }
}
